package com.acmelabs.inbox;

import java.util.List;
import java.util.Vector;

import adhoc.tel.fer.hr.R;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class NotificationListAdapter extends BaseAdapter {

	private LayoutInflater layoutInflater;
	private List<NotificationItem> conversations;
	private Vector<View> listItems = new Vector<View>();

	public NotificationListAdapter(LayoutInflater layoutInflater,
			List<NotificationItem> conversations) {
		this.layoutInflater = layoutInflater;
		this.conversations = conversations;
	}

	@Override
	public boolean areAllItemsEnabled() {
		return true;
	}

	@Override
	public boolean isEnabled(int position) {
		return true;
	}

	@Override
	public int getCount() {
		return conversations.size();
	}

	public List<NotificationItem> getConversations() {
		return conversations;
	}

	public View getItemView(int position) {
		return position >= listItems.size() ? null : listItems
				.elementAt(position);
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		NotificationListItemParts notificationListItemParts;
		NotificationItem conversation = conversations.get(position);
		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.notification_inbox_list_item, null);
			notificationListItemParts = new NotificationListItemParts();
			notificationListItemParts.title = (TextView) convertView
					.findViewById(R.id.title);
			notificationListItemParts.description = (TextView) convertView
			.findViewById(R.id.dt);
			
			notificationListItemParts.dateTime = (TextView) convertView
					.findViewById(R.id.dateTime);
			notificationListItemParts.notiItem = conversation;
			convertView.setTag(notificationListItemParts);
		} else {
			notificationListItemParts = (NotificationListItemParts) convertView.getTag();
		}

		notificationListItemParts.title.setText(conversation.title);
		notificationListItemParts.dateTime.setText(conversation.time);
		notificationListItemParts.description.setText(conversation.details);
		notificationListItemParts.dateTime.setVisibility(ViewGroup.VISIBLE);

		if (listItems.size() < getCount()) {
			listItems.setSize(getCount());
		}
		listItems.setElementAt(convertView, position);
		return convertView;
	}
}
